import { t } from "@/locales";
import { FormRule, PrimaryTableCol, SelectProps, Tag } from "tdesign-vue-next";
import { SearchParamsProps } from "@/components/s-table/type";
import { fileView } from "@/api/upload";
import SImageView from '@/components/ImageView/index.vue';
/**
 * 搜索表单项
 */
export const searchParams: SearchParamsProps[] = [
    {
        label: t('pages.motion.name'),
        key: 'name',
    },
    {
        label: t('pages.motion.beneficialPosition'),
        key: 'beneficialPosition',
    },
];

/**
 * 表格列
 */
export const COLUMNS: PrimaryTableCol[] = [
    {
        title: t('pages.motion.name'),
        ellipsis: true,
        align: 'center',
        colKey: 'name',
    },
    {
        title: t('pages.motion.type'),
        ellipsis: true,
        align: 'center',
        colKey: 'type',
        cell:(h, {row}) => {
            if(row.type == 0) {
                return t('pages.motion.typeOption.sports')
            } else if(row.type == 1) {
                return t('pages.motion.typeOption.motion')
            } else if(row.type == 2) {
                return t('pages.motion.typeOption.sportsNew')
            }
            }
    },
    {
        title: t('pages.motion.applicableAge'),
        ellipsis: true,
        align: 'center',
        colKey: 'applicableAge',
        cell: (h, { row }) => {
            if(row.applicableAge){
                try{
                    return h(SImageView, { images: [fileView(row.applicableAge)],width: 15,height: 15 });
                }catch(e){
                    return h('div', '-');
                }
            }
            return '-';
        }
    },
    {
        title: t('pages.motion.beneficialPosition'),
        ellipsis: true,
        align: 'center',
        colKey: 'beneficialPosition',
    },
    {
        title: t('pages.motion.introduction'),
        ellipsis: true,
        align: 'center',
        colKey: 'introduction',
    },
    {
        title: t('pages.motion.status'),
        ellipsis: true,
        align: 'center',
        colKey: 'status',
        cell: (h, {row}) => h(Tag,
            {theme:row.status == 0 ? 'danger' : row.status == 1 ? 'primary' : 'warning'
            },t(`pages.motion.statusOptions.${row.status==0 ? 'Unpublished' : row.status==1? 'Published' : 'rescinded'}`))
        
    },
    {
        title: t('pages.motion.pushTime'),
        ellipsis: true,
        align: 'center',
        colKey: 'pushTime',
        cell: (h, { row }) => {
            if (row.pushTime) {
                const date = new Date(row.pushTime);
                const options = { 
                    year: 'numeric', 
                    month: '2-digit', 
                    day: '2-digit', 
                    hour: '2-digit', 
                    minute: '2-digit', 
                    second: '2-digit', 
                    hour12: false 
                };
                const formatter = new Intl.DateTimeFormat('zh-CN', options);
                return formatter.format(date);
            }
            return '-';
        }
    },
    {
        title: t('pages.motion.remarks'),
        ellipsis: true,
        align: 'center',
        colKey: 'remarks',
    },
];

/**
 * 表单校验规则
 */
export const FORM_RULES: Record<string, FormRule[]> = {
    name: [{required: true, message: t('pages.motion.required.name'), type: 'error', trigger: 'all'}],
    type: [{required: true, message: t('pages.motion.required.type'), type: 'error', trigger: 'all'}],
    introduction: [{required: true, message: t('pages.motion.required.introduction'), type: 'error', trigger: 'all'}],
}

/**
 * 配置类型下拉选项（type）
 */
export const typeOption: SelectProps['options'] = [
    {
        label: t('pages.motion.typeOption.sports'),
        value: 0
    },
    {
        label: t('pages.motion.typeOption.motion'),
        value: 1
    },
    {
        label: t('pages.motion.typeOption.sportsNew'),
        value: 2
    }
]

export const statusOption: SelectProps['options'] = [
    {
        label: t('pages.motion.statusOptions.Unpublished'),
        value: 0
    },
    {
        label: t('pages.motion.statusOptions.Published'),
        value: 1
    },
    {
        label: t('pages.motion.statusOptions.rescinded'),
        value: 2
    }
]